package code.structure.module.friends
{
	import flash.utils.Dictionary;
	
	import code.main.core.mvc.YModelData;
	import code.structure.module.friends.data.FriendData;
	
	/**
	 *好友控制器
	 * @author CYoung
	 * 2014-7-15下午9:54:29
	 */
	public class FriendsModel extends YModelData
	{
		/** APP好友 */
		private var _appFriends:Array;
		/** facebook好友 */
		private var _fbFriends:Array;
		/** 所有好友的数据 */
		private var _dectionary:Dictionary;
		/** 读取好友扩展数据时的游标 */
		private var _readIndex:int;
		
		public function FriendsModel()
		{
			super();
		}
		/**
		 * 读取facebook好友
		 */		
		public function readFacebookFriends():void
		{
			_appFriends = [];
			_fbFriends = [];
			ReadFaceBookFriends.getInstance().readFriends(analysisFacebookData, null);
		}
		/**
		 * 解析好友的基础数据
		 */		
		private function analysisFacebookData(data:Object):void
		{
			for each(var d:Object in data){
				var friend:FriendData = new FriendData(d);
				friend.installed == true?_appFriends.push(friend):_fbFriends.push(friend);
				_dectionary[friend.id] = friend;
			}
			requestExpandData(0, 50);
		}
		/**
		 * 请求后台读取扩展数据
		 * @param start 起始位置
		 * @param end 结束位置
		 */		
		private function requestExpandData(start:int, end:int):void{
			if(end >= _appFriends.length) end = _appFriends.length;
			var idList:Array = [];
			for (var i:int = start; i < end; i++) {
				idList.push(_appFriends[i].id);
			}
			//_netManager.addNetCmdDetail(
			_readIndex = end;
		}
		/**
		 * 解析好友的扩展数据 
		 */		
		private function analysisExpandData(data:Object):void
		{
			for each (var i:Object in data) {
				_dectionary[i.id].analysisExpandData(i);
			}
			if(_appFriends.length > _readIndex)
				requestExpandData(_readIndex, requestExpandData + 50);
			else
				trace("通知好友加载完成, 显示好友列表"); 
		}
	}
}